using System;
using miPics.Common;
using miPics.Rss;


public partial class _Administration_EditGallery : miPics.BasePage.BaseAdministrationGallery
{
	private bool ValidGallery = false;
	private short GalleryID;

	protected void Page_Load(object sender, EventArgs e)
	{
		// set menu administration task
		Master.SetMenuAdministrationTask("Edit gallery");

		// load gallery we are editing
		this.GalleryID = ToolKit.ReturnShort(Request.QueryString["id"]);

		// get gallery details into datatable through table adapter
		miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItemTableAdapter = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();
		miPicsDataAccess.GalleryItemDataTable GalleryItemDataTable = GalleryItemTableAdapter.GetData(this.GalleryID, this.IsLoggedIn);

		this.GalleryLoaded = (GalleryItemDataTable.Count == 1);

		if (GalleryItemDataTable.Count == 1)
		{
			this.ValidGallery = true;

			// if not postback, populate fields with gallery data
			if (!this.IsPostBack)
			{
				miPicsDataAccess.GalleryItemRow GalleryItemRow = (miPicsDataAccess.GalleryItemRow)GalleryItemDataTable.Rows[0];

				// pass image details to controls
				if (string.IsNullOrEmpty(GalleryItemRow.Thumbnail))
				{
					// no gallery image set, show generic image
					_GalleryImage.Src = Config.General.NoGalleryImageSrc;
					_GalleryImage.Width = Config.Image.WidthThumbnail;
					_GalleryImage.Height = Config.Image.HeightThumbnail;
					_GalleryImage.Alt = Config.General.NoGalleryImageName;
				}
				else
				{
					// show gallery image
					_GalleryImage.Src = "~/_Resource/Image/" + GalleryItemRow.Thumbnail;
					_GalleryImage.Width = GalleryItemRow.ThumbnailWidth;
					_GalleryImage.Height = GalleryItemRow.ThumbnailHeight;
					_GalleryImage.Alt = ToolKit.GetAltImageName(GalleryItemRow.ThumbnailName);
				}

				// pass gallery description details
				_GalleryName.Value = GalleryItemRow.Name;
				_GalleryDate.Value = GalleryItemRow.GalleryDate.ToString(GalleryDateFormat);
				_GalleryDescription.Value = GalleryItemRow.Description;

				// get gallery image count
				_GalleryImageCount.InnerText = GalleryItemTableAdapter.GetImageCount(this.GalleryID, true).ToString();

				// set the enabled checkbox
				_GalleryEnabled.Checked = GalleryItemRow.Enabled;
			}
		}
	}

	protected void UpdateGallery(object sender, EventArgs e)
	{
		// update image details to database
		if (this.ValidGallery)
		{
			// validate form details
			string GalleryName = ToolKit.TrimToLength(_GalleryName.Value, 100);
			string GalleryDescription = ToolKit.TrimToLength(_GalleryDescription.Value, 1000);

			if (GalleryName == "")
				_FormErrorList.AddError("Please enter a name for the gallery");

			this.ValidateGalleryDate(_GalleryDate.Value);

			if (GalleryDescription == "")
				_FormErrorList.AddError("Please enter a description for the gallery");

			// if no errors, commit
			if (!_FormErrorList.IsError)
			{
				// commit details to gallery record
				miPicsDataAccessTableAdapters.GalleryItemTableAdapter GalleryItem = new miPicsDataAccessTableAdapters.GalleryItemTableAdapter();

				GalleryItem.UpdateItem(
					this.GalleryID,
					this.ParsedGalleryDate,
					GalleryName,
					_GalleryEnabled.Checked,
					GalleryDescription);

				// update gallery rss
				WriteRss GalleryRss = new WriteRss(this.Request);
				GalleryRss.SaveRss();

				// redirect back to home
				Response.Redirect("~/");
			}
		}
	}

	protected void CancelUpdate(object sender, EventArgs e)
	{
		// redirect back to home
		Response.Redirect("~/");
	}

	protected void SetCurrentDate(object sender, EventArgs e)
	{
		_GalleryDate.Value = this.GetNewGalleryDate;
	}

	private bool GalleryLoaded
	{
		set
		{
			_ErrorMessage.Visible = !value;
			_Interface.Visible = value;
		}
	}

	protected override string GalleryDateError
	{
		set { _FormErrorList.AddError(value); }
	}
}
